package com.geekaca.exam;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Test4ShopSystem {
    static Scanner scanner = new Scanner(System.in);
    static ArrayList<Commodity> commodityList = new ArrayList<>();

    public static void main(String[] args) {
        initCommodities();
        mainMenu();
    }

    public static void mainMenu() {

        while (true) {
            System.out.println("===============欢迎进入商品管理系统===============");
            //系统主菜单
            System.out.println("1. 商品列表");
            System.out.println("2. 商品录入");
            System.out.println("3. 商品查询");
            System.out.println("4. 统计信息");
            System.out.println("5. 退出");
            System.out.println("请您选择操作：");
            Scanner sc = new Scanner(System.in);
            //命令
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    // 商品列表
                    showCommodity();
                    break;
                case 2:
                    // 商品录入
                    addCommodity();
                    break;
                case 3:
                    // 商品查询
                    queryCommodity();
                    break;
                case 4:
                    // 统计信息
                    totalInfo();
                    break;
                case 5:
                    // 退出
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }

    private static void totalInfo() {
        int totlCount = 0;
        double totalPrice = 0;
        for (int i = 0; i < commodityList.size(); i++) {
            totlCount += commodityList.get(i).getCount();
            totalPrice += commodityList.get(i).getCount() * commodityList.get(i).getPrice();
        }

        System.out.println("商品总数为: " + totlCount + "; 商品总价为: " + totalPrice);
        System.out.println("按x键回车返回主菜单...");
        scanner.next();
    }

    private static void queryCommodity() {
        System.out.println("=========商品查询=========");
        System.out.println("输入商品名称:");
        String name = scanner.next();
        // 设置日期显示格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        for (int i = 0; i < commodityList.size(); i++) {
            if (commodityList.get(i).getCommodityName().contains(name)) {
                Commodity commodity = commodityList.get(i);
                String comodityInfo = commodity.getCommodityInfo();
                // 处理超过10字符的商品简介
                if (comodityInfo.length() > 10) {
                    comodityInfo = comodityInfo.substring(0,10) + "...";
                }
                // 处理日期对象
                String commodityDate = sdf.format(commodity.getDate());


                System.out.println("商品编号:" + commodity.getCommodityId() + "; 商品名称: " + commodity.getCommodityName() +
                        "; 商品简介: " + comodityInfo + "; 商品价格: " + commodity.getPrice() + "; 商品数量: "
                        + commodity.getCount() + "; 入库时间: " + commodityDate);
            }
        }

        System.out.println("按x键回车返回主菜单...");
        scanner.next();


    }

    private static void addCommodity() {
        System.out.println("=========请录入商品信息=========");
        System.out.println("商品编号:");
        String no = scanner.next();
        System.out.println("商品名称:");
        String name = scanner.next();
        System.out.println("商品简介:");
        String info = scanner.next();
        System.out.println("商品价格:");
        double price = scanner.nextDouble();
        System.out.println("商品数量:");
        int count = scanner.nextInt();
        Date date = new Date();

        Commodity commodity = new Commodity();
        commodity.setCommodityId(no);
        commodity.setCommodityName(name);
        commodity.setCommodityInfo(info);
        commodity.setCommodityPrice(price);
        commodity.setCount(count);
        commodity.setDate(date);

        commodityList.add(commodity);
        System.out.println("录入完成,返回主页");


    }

    private static void showCommodity() {
        for (int i = 0; i < commodityList.size(); i++) {
            System.out.println(commodityList.get(i).getCommodityId() + ". " + commodityList.get(i).getCommodityName());
        }
        System.out.println("按x键回车返回主菜单...");
        scanner.next();

    }

    private static void initCommodities() {
        Commodity commodity1 = new Commodity();
        commodity1.setCommodityId("1");
        commodity1.setCommodityName("罗技术鼠标");
        commodity1.setCommodityInfo("一种带滚轮并能水分翻页的无线鼠标,支持5档分辨率切换,软件定制灯光等功能.");
        commodity1.setCommodityPrice(299.99);
        commodity1.setCount(5);
        commodity1.setDate(new Date());

        Commodity commodity2 = new Commodity();
        commodity2.setCommodityId("2");
        commodity2.setCommodityName("香酥猪油渣");
        commodity2.setCommodityInfo("一种食品,百年工艺,香酥美味,保鲜包装.");
        commodity2.setCommodityPrice(25);
        commodity2.setCount(10);
        commodity2.setDate(new Date());

        commodityList.add(commodity1);
        commodityList.add(commodity2);
    }
}
